package com.harmontronics.hanma.paas.paas.domain.app.repository.persistence;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.harmontronics.hanma.paas.paas.consts.NumberEnum;
import com.harmontronics.hanma.paas.paas.domain.app.entity.valueobject.Fields;
import com.harmontronics.hanma.paas.paas.domain.app.repository.mapper.HmProcessMapper;
import com.harmontronics.hanma.paas.paas.domain.app.repository.po.HmProcessPO;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository("hmProcessRepository")
public class HmProcessRepository extends ServiceImpl<HmProcessMapper, HmProcessPO> {

    public IPage<HmProcessPO> selectPage(QueryWrapper<HmProcessPO> queryWrapper, Page<HmProcessPO> page) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
     * 查询目录下 流程数（包含版本）
     *
     * @param folderId
     * @return
     */
    public int countByFolderId(Long folderId) {
        QueryWrapper<HmProcessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Fields.FOLDER_ID, folderId);
        return baseMapper.selectCount(queryWrapper);
    }

    public void removeByKey(String key) {
        QueryWrapper<HmProcessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Fields.KEY_DB, key);
        baseMapper.delete(queryWrapper);
    }


    public List<HmProcessPO> findByName(String name) {
        QueryWrapper<HmProcessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Fields.NAME, name);
        return baseMapper.selectList(queryWrapper);
    }

    public List<HmProcessPO> findByDeploymentIdIn(List<String> deploymentIds) {
        QueryWrapper<HmProcessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(Fields.DEPLOYMENT_ID, deploymentIds);
        return baseMapper.selectList(queryWrapper);
    }

    public List<HmProcessPO> findByKey(String key) {
        QueryWrapper<HmProcessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Fields.KEY_DB, key);
        queryWrapper.orderByDesc(Fields.VERSION_DB);
        return baseMapper.selectList(queryWrapper);
    }


    public List<HmProcessPO> findUnPackedActivated() {
        QueryWrapper<HmProcessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Fields.ACTIVATED, NumberEnum.ONE.getNum());
        queryWrapper.eq(Fields.PACKED, NumberEnum.ZERO.getNum());
        return baseMapper.selectList(queryWrapper);
    }

    public List<HmProcessPO> findUsable(String modelCode) {
        QueryWrapper<HmProcessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Fields.MODEL_CODE, modelCode);
        queryWrapper.eq(Fields.ACTIVATED, NumberEnum.ONE.getNum());
        return baseMapper.selectList(queryWrapper);
    }

    public List<HmProcessPO> findActivated() {
        QueryWrapper<HmProcessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Fields.ACTIVATED, NumberEnum.ONE.getNum());
        return baseMapper.selectList(queryWrapper);
    }
}